package com.ruoyi.report.forms.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.report.forms.domain.ReportFormsTemplate;
import com.ruoyi.report.forms.service.IReportFormsEditService;
import com.ruoyi.report.forms.service.IReportFormsTemplateService;
import com.ruoyi.report.forms.util.ExportExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/edit")
public class ReportFormsEditController {

    @Autowired
    private IReportFormsEditService reportFormsEditService;

    @Autowired
    private IReportFormsTemplateService reportFormsTemplateService;

    @GetMapping("/listColName")
    public List<String> listColName(String tableName) {
        List<String> list = reportFormsEditService.listColName(tableName);
        return list;
    }

    @PostMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, ReportFormsTemplate reportFormsTemplate) {
        ReportFormsTemplate template = reportFormsTemplateService.selectReportFormsTemplateByTemplateId(reportFormsTemplate.getTemplateId());
        ExportExcel export = new ExportExcel(template);
        export.GetDataConditions();
        export.tableData = reportFormsEditService.listDataByTemplate(export.tableName, export.colNameList, null, null);
        export.CreateExcel();
        export.OutputExcel(response);
    }

    @PostMapping("/exportExcelByTemplate")
    public void exportExcelByTemplate(HttpServletResponse response, ReportFormsTemplate reportFormsTemplate) {
        ExportExcel export = new ExportExcel(reportFormsTemplate);
        export.GetDataConditions();
        export.tableData = reportFormsEditService.listDataByTemplate(export.tableName, export.colNameList, null, null);
        export.CreateExcel();
        export.OutputExcel(response);
    }

    @GetMapping(value = "/{templateId}")
    public Map<String, Object> getDataById(@PathVariable("templateId") String templateId) {
        Map<String, Object> result = new HashMap<>();
        ReportFormsTemplate template = reportFormsTemplateService.selectReportFormsTemplateByTemplateId(templateId);
        ExportExcel export = new ExportExcel(template);
        export.GetDataConditions();
        export.tableData = reportFormsEditService.listDataByTemplate(export.tableName, export.colNameList, null, null);
        result.put("template", template);
        result.put("rows", export.tableData);
        return result;
    }
}
